<template>
    <div class="goods-list">
        <!-- <router-link class="good-item" v-for="item in goodsList" :key="item.commodityId"
                    :to="'/home/goodsinfo/'+item.commodityId" tag="div">
            <img :src="'http://'+item.homePicThumbUrl" alt="">
            <h1 class="title">{{ item.title }}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">
                        ￥{{ item.sellPrice }}
                    </span>
                    <span class="old">
                        ￥{{ item.stickerPrice }}
                    </span>
                </p>
                <p class="sell">
                    <span class="click">关注度{{ item.click}}</span>
                    <span class="stock">库存{{ item.stock }}{{ item.commodityUom }}</span>
                </p>
            </div>
        </router-link> -->

        <!-- 在网页中有两种跳转方式： -->
        <!-- 方式1： 使用a标签的形式叫做标签跳转 -->
        <!-- 方式2： 使用window.location.href的形式，叫做编程式跳转 -->
        <div class="good-item" v-for="item in goodsList" :key="item.commodityId" @click="goDetail(item.commodityId,item.homePicTUmbUrl)">
            <img :src="'http://'+item.homePicThumbUrl" alt="">
            <h1 class="title">{{ item.title }}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">
                        ￥{{ item.sellPrice }}
                    </span>
                    <span class="old">
                        ￥{{ item.stickerPrice }}
                    </span>
                </p>
                <p class="sell">
                    <span class="click">关注度{{ item.click}}</span>
                    <span class="stock">库存{{ item.stock }}{{ item.commodityUom }}</span>
                </p>
            </div>
        </div>
        <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
import {
        Toast
    } from 'mint-ui'
export default {
    data() {
        return {
            goodsList: [],
            pageSize: 1,
            toastInstanse: ''
        }
    },
    created(){
        this.getGoodsList();
    },
    methods: {
        getGoodsList() {
            this.$http.get('front/goods?pageSize='+this.pageSize).then(result => {
                if(result.body.code ===1){
                    this.goodsList = result.body.data;
                }else{
                        Toast('与服务器断开连接')
                    }
            })
        },
        getMore() {
            this.pageSize++;
            this.$http.get('front/goods?pageSize='+this.pageSize).then(result => {
                if(result.body.code ===1){
                    if(result.body.data.length === 0) {
                        this.show();
                        setTimeout(() => {
                            this.toastInstanse.close()
                        }, 1000)
                    }else{
                        this.goodsList = this.goodsList.concat(result.body.data);
                    }
                }
            })
        },
        goDetail(id,pic) {
            // 使用js的形式进行路由导航
            // 注意：一定要区分this.$route 和 this.$router
            // 其中，this.$route是路由[参数对象]，所有路由中的参数，param和query都属于他；
            // 其中，this.$router 是一个路由[导航对象]，用它可以方便地使用js代码实现路由的前进
            // 后退、跳转到新的url地址
            // 1.最简单的
            // this.$router.push('/home/goodsinfo/'+id);
            // 2.传递对象
            // this.$router.push({path:'/home/goodsinfo/'+id});
            // 3.传递命名的路由
            this.$router.push({name: "goodsinfo",params: {id,pic}});
        },
        show() {
                this.toastInstanse = Toast({
                    message: '没有更多商品数据',
                    duration: -1,
                    position: 'middle',
                    iconClass: 'glyphicon glyphicon-remove',
                    className: 'myToast' // 自定义Toast的样式，需要自己提供一个类名
                })
        },
    }
}
</script>

<style lang="scss" scoped>
.goods-list{
    display: flex;
    flex-wrap: wrap;
    padding: 8px;
    justify-content: space-between;
    .good-item{
        padding: 2px;
        width: 49%;
        border: 1px solid #ccc;
        box-shadow: 0 0 8px #ccc;
        margin: 3px 0;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        min-height: 293px;
        img{
            width: 100%;
        }
        .title{
            font-size: 14px;

        }

        .info{
            background-color: #eee;
            p{
                margin: 0;
                padding: 5px;
            }
            .price{
                .now{
                    color: red;
                    font-weight: bold;
                    font-size: 16px;
                }
                .old{
                    text-decoration: line-through;
                    font-size: 10px;
                    margin-left: 10px;
                }
                
            }

            .sell{
                display: flex;
                justify-content: space-between;
                font-size: 14px;
                .click{
                    color:rgb(240, 152, 167);
                }
                
            }
        }
    }
}
</style>